package com.mebius.junit.http;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

public class ImoocDownload {

	public static void main(String[] args) {
		DownloadVideo dv = new DownloadVideo();
		
		new Thread(dv).start() ;
	}
	
}

class DownloadVideo implements Runnable {
	
	Pattern title = Pattern.compile("(<a\\s*href=\".*?\"\\s*class=\"chaptername\">(.*)</a>)") ;
	Pattern link = Pattern.compile("(<video\\s*.*\\s*src=\"(.*?)\">\\s?</video>)") ;
	
	@Override
	public void run() {
		
		String url = "http://www.imooc.com/video/7789" ;
		String resource = getHttpResource(url) ;
		System.out.println(resource);
		Matcher matcherTitle = title.matcher(resource) ;
		Matcher matcherLink = link.matcher(resource) ;
		
		while(matcherTitle.find()) {
			System.out.println(matcherTitle.group(2).replaceAll("[\n|\r|\t|\\s*]", "").replaceAll("<iclass=\"iconicon-menu\"></i>", ""));
			System.out.println(matcherLink.group(2).replaceAll("[\n|\r|\t]", ""));
		}
		
	}
	
	public void download(String name, String url) {
		//String url = "http://video.mukewang.com/abd539cc-0bb4-4406-a74f-44f4142b88f6/H.mp4" ;
		CloseableHttpClient httpClient = HttpClients.createDefault() ;
		HttpGet httpGet = new HttpGet(url) ;
		CloseableHttpResponse response = null ;
		
		InputStream is = null ;
		BufferedInputStream bis = null ;
		FileOutputStream fos = null ;
		BufferedOutputStream bos = null ;
		try {
			response = httpClient.execute(httpGet) ;
			HttpEntity entity = response.getEntity();
			
			is = entity.getContent() ;
			bis = new BufferedInputStream(is) ;
			
			fos = new FileOutputStream(new File("c:/h.mp4")) ;
			bos = new BufferedOutputStream(fos) ;
			int len = 0;				
			byte buf[] = new byte[((Long)entity.getContentLength()).intValue()] ;
			while((len=bis.read(buf)) != -1){
				bos.write(buf, 0, len);
			}
			bos.flush(); 
			fos.flush();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
		    try {
				response.close();
				bos.close();
				fos.close();
				bis.close();
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public String getHttpResource(String url) {
		CloseableHttpClient httpClient = HttpClients.createDefault() ;
		HttpGet httpGet = new HttpGet(url) ;
		CloseableHttpResponse response = null ;
		
		StringBuffer strBuf = new StringBuffer() ;
		
		InputStream is = null ;
		BufferedInputStream bis = null ;
		try {
			response = httpClient.execute(httpGet) ;
			HttpEntity entity = response.getEntity();
			
			is = entity.getContent() ;
			bis = new BufferedInputStream(is) ;
			
			byte buf[] = new byte[1024] ;
			while(bis.read(buf) != -1){
				String str = new String(buf) ;
				strBuf.append(str) ;
			}
			
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
		    try {
				response.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return strBuf.toString() ;
	}
	
}
